import { mapState } from 'vuex'
import { COLUMN_PERCENT, GLOBAL_POST_TYPES, CONTROL_TYPES } from '@/common/const'
import { isWeibo, isHasOwnProperty } from '@/plugins/utils'
import { bgPositionStyle, convertCss } from '@/store/utils'
import { STATISTIC_DATA_TYPES_MAP } from '@/common/events'
import { COMPONENT_TYPES } from '@/common/const-wb'
import { isJSON, getCompIdByParam } from '@/plugins/utils'
import { getPxOVwByValue, getPxOrVw, getMainListEventByEvents, getMainWbDataEventByEvents } from '@/store/utils'
import { EventBus } from '@/store/index'

export const utils = {
  data () {
    return {
      lists_: [],//这个代码很重要，不要删除，为了刷新web端ui,
      wbData_: null,
      voteItemData_: null,
      userItemData_: null,
      repostItemData_: null,
      rankFriendItemData_: null,
      numberItemData_: null,
    }
  },
  props: {
    item: Object,
    pageId: {
      type: Number,
      default: -1,
    },
    popId: {
      type: Number,
      default: -1,
    },
    index: {
      type: Number,
      default: -1,
    },//列表item的index
  },
  created () {
    const data_ = {
      COLUMN_PERCENT: COLUMN_PERCENT,
      GLOBAL_POST_TYPES: GLOBAL_POST_TYPES,
    };
    for(let key in data_) {
      this[key] = data_[key];
    }
  },
  computed: {
    ...mapState({
      allPages: state => state.allPages ? state.allPages : state.components.pages,
      controls: state => state.controls ? state.controls : state.wbComponents.controls,
      componentMap: state => state.componentMap ? state.componentMap : state.components.componentMap,
      canvasImgComponentMap: state => state.canvasImgComponentMap,
      shareData: (state) => state.shareData,
      animateStatus: state => state.animateStatus,
      pageIndex: state => state.pageIndex,
      siteInfo: state => state.siteInfo,
      userInfo: state => state.userInfo,
      formValues: state => state.formValues,
      statePopId: state => state.popId,
      globalIsPost: state => state.globalIsPost,
      siteAttrs: state => state.attrs,
      siteStyles: state => state.styles,
      timeObj: state => state.timeObj,
      bindData: state => state.bindData,
      fileUploadData: state => state.fileUploadData,
      appid: state => state.attrs.wb_appkey,
      activityInfo: state => state.activityInfo,
    }),
    getItemIndex(){
      let index = 0;
      if(this.index >= 0){
        index = this.index;
      } else {
        const relateAttrCompId = getCompIdByParam(this.commonAttr.relateAttrCompId)
        if(relateAttrCompId){
          const relateCompData = this.componentMap[relateAttrCompId];
          if(relateCompData.commonAttr.itemIndex >= 0) {
            index = relateCompData.commonAttr.itemIndex
          }
          if(this.commonAttr.itemIndex >= 0) {
            index = this.commonAttr.itemIndex
          }
        } else {
          if(this.commonAttr.itemIndex >= 0) {
            index = this.commonAttr.itemIndex
          }
        }
      }
      return index >= 0 ? index : 0;
    },
    getBlogText(){
      let textStr = ''
      if(this.repostItemData || this.userItemData){
        const itemData = this.repostItemData || this.userItemData;
        let textPrefix = '';
        if(this.commonAttr.isCustomText){
          textPrefix = this.commonAttr.textPrefix;
        }
        if(this.isH5Edit){
          if(itemData.push_text){
            textStr = textPrefix + itemData.push_text
          }else{
            textStr = textPrefix
          }
        }else{
          if(this.dataKeys && this.dataKeys.text){
            if(itemData.text){
              textStr = textPrefix + itemData.text
            }else{
              textStr = textPrefix
            }
          }else{
            textStr = textPrefix + itemData.push_text
          }
        }
      }else{
        textStr = this.getItem ? this.getItem.text : ''
      }
      return textStr;
    },
    nameFontSize(){
      let fontSize = this.commonAttr.fontSize_ ? this.commonAttr.fontSize_ : this.commonAttr.fontSize
      fontSize += 6
      return {
        "fontSize": getPxOVwByValue(fontSize)
      }
    },
    nameSuffixFontSize(){
      let fontSize = this.commonAttr.fontSize_ ? this.commonAttr.fontSize_ : this.commonAttr.fontSize
      fontSize = fontSize/3*2
      return {
        "fontSize": getPxOVwByValue(fontSize)
      }
    },
    styles(){
      let styles_ = _.cloneDeep(this.item.styles)
      if(_.isArray(styles_)){
        styles_ = {}
      }
      let commonAttr = this.item.commonAttr
      this.computeStyles(styles_, commonAttr)
      return styles_
    },
    btnStyles(){
      let unit = getPxOrVw()
      let styles_ = _.cloneDeep(this.item.btn.styles);
      let commonAttr = this.item.btn.commonAttr
      this.computeStyles(styles_, commonAttr)
      let bgUrl = this.item.btn.bgUrl
      if(bgUrl && bgUrl.length > 0){
        styles_.backgroundImage = 'url(' + bgUrl + ')';
        if(commonAttr.width > 0){
          styles_.width = commonAttr.width + unit;
        }
        if(commonAttr.heigh > 0){
          styles_.height = commonAttr.height + unit
        }
      }
      if(commonAttr.btnPosition && commonAttr.btnPosition.length > 0){
        let btnPosition = commonAttr.btnPosition.split(',');
        if(btnPosition.length > 0 && btnPosition[0]){
          styles_.position = "absolute";
          styles_.left = getPxOVwByValue(btnPosition[0]);
          styles_.top = getPxOVwByValue(btnPosition[1]);
        }
      }
      return styles_
    },
    commonAttr() {
      return this.item && this.item.commonAttr
    },
    dataKeys(){
      return this.item.dataKeys
    },
    getVariableItem(){
      let item
      if(this.isWbLists){
        item = this.getControlItem
      }else{
        item = this.getItem
      }
      return item
    },
    getItem(){
      return this.item.lists ? this.item.lists[this.getItemIndex] : null
    },
    getItemControls(){
      if(this.item){
        return this.controls[this.item.id]
      }else{
        return this.controls[this.currentPage.id]
      }
    },
    getControlItem(){
      return this.wbLists[this.getItemIndex]
    },
    repostItemData(){
      if(this.getItemControls){
        let itemData = this.getItemControls[CONTROL_TYPES.wb_reposts]
        if(itemData){
          this.repostItemData_ = itemData.data.elements[this.getItemIndex]
        }
      }
      return this.repostItemData_
    },
    userItemData(){
      if(this.getItemControls){
        let itemData = this.getItemControls[CONTROL_TYPES.wb_user]
        if(itemData){
          this.userItemData_ = itemData.data.elements[this.getItemIndex]
        }
      }
      return this.userItemData_
    },
    rankFriendItemData(){
      if(this.getItemControls){
        let itemData = this.getItemControls[CONTROL_TYPES.wb_rank_friend]
        if(itemData && itemData.data.elements[this.getItemIndex]){
          this.rankFriendItemData_ = itemData.data.elements[this.getItemIndex]
        }
      }
      return this.rankFriendItemData_
    },
    voteItemData(){
      if(this.getItemControls){
        let itemData
        itemData = this.wbListOrLists[this.getItemIndex]
        const key = itemData.voteId || itemData.mid || itemData.uid
        let voteData = this.getItemControls[CONTROL_TYPES.vote]
        if(voteData){
          this.voteItemData_ = voteData.data.elements[key]
        }
      }
      return this.voteItemData_
    },
    numberItemData(){
      if(this.getItemControls){
        let itemData = this.getItemControls[CONTROL_TYPES.number] || this.getItemControls[CONTROL_TYPES.wb_general_num]
        if(itemData){
          this.numberItemData_ = itemData
        }
      }
      return this.numberItemData_
    },
    wbData(){
      const relateAttrCompId = getCompIdByParam(this.commonAttr.relateAttrCompId);
      let controlData;
      if(relateAttrCompId){
        const relateCompData = this.componentMap[relateAttrCompId];
        const relateMainEvent = getMainWbDataEventByEvents(relateCompData.events);
        controlData = relateMainEvent && this.controls[relateAttrCompId] && this.controls[relateAttrCompId][relateMainEvent.controlId];
        if(controlData){
          if(!isHasOwnProperty(controlData.data, "elements")){
            this.wbData_ = controlData.data
          }
        }
      }else{
        let event = getMainWbDataEventByEvents(this.item.events)
        if(event && this.getItemControls){
          controlData = this.getItemControls[event.controlId]
          if(controlData){
            if(!isHasOwnProperty(controlData.data, "elements")){
              this.wbData_ = controlData.data
            }
          }
        }else{
          this.wbData_ = null
        }
      }
      return this.wbData_
    },
    wbListOrLists(){
      if(this.isWbLists){
        this.lists_ = this.wbLists
      }else{
        this.lists_ = this.lists
      }
      if(this.commonAttr.isFooter){
        this.lists_ =  _.slice(this.lists_, 0, this.commonAttr.itemNum);
      }
      return this.lists_
    },
    lists(){
      const relateAttrCompId = getCompIdByParam(this.commonAttr.relateAttrCompId);
      if(relateAttrCompId){
        const relateCompData = this.componentMap[relateAttrCompId];
        return relateCompData.lists;
      }else{
        return this.item.lists
      }
    },
    wbLists(){
      let wbLists_ = []
      const relateAttrCompId = getCompIdByParam(this.commonAttr.relateAttrCompId);
      let controlData;
      if(relateAttrCompId){
        const relateCompData = this.componentMap[relateAttrCompId];
        if(relateCompData) {
          const relateMainEvent = getMainListEventByEvents(relateCompData.events);
          controlData = relateMainEvent && this.controls[relateAttrCompId] && this.controls[relateAttrCompId][relateMainEvent.controlId];
          if(controlData && controlData.data.elements){
            wbLists_ = controlData.data.oriElements || controlData.data.elements;
          }
        }
      }else{
        let event = this.item && getMainListEventByEvents(this.item.events);
        if(event && this.getItemControls){
          controlData = this.getItemControls[event.controlId];
          if(controlData){
            if(controlData.data.elements){
              wbLists_ = controlData.data.elements;
            }
          }
        }
      }
      return wbLists_
    },
    isWbLists(){
      let wbLists_ = this.wbLists
      if(_.isArray(wbLists_) && wbLists_.length > 0){
        return true
      }else{
        return false
      }
    },
    btnTitle(){
      return this.item.btn.bgUrl && this.item.btn.bgUrl.length > 0 ? ' ' : this.item.btn.commonAttr.title
    },
    getTextStyle(){
      let unit = getPxOrVw();
      let styles_ = {};
      let lineNum = this.item.commonAttr.lineNum;
      let lineHeight = this.item.styles.lineHeight;
      let fontSize = this.item.commonAttr.fontSize * 0.8;
      styles_.fontSize = fontSize + unit;
      // styles_.color = this.item.styles.color;
      let textHeight = fontSize*lineHeight*lineNum;

      styles_['-webkit-line-clamp'] = lineNum;
      styles_.minHeight = textHeight + unit;
      // delete styles_.backgroundColor;

      if(this.commonAttr.customStyle){
        const customStyle = JSON.parse(this.commonAttr.customStyle);
        if(customStyle){
          if(customStyle.textMarginTop){
            styles_.marginTop = getPxOVwByValue(customStyle.textMarginTop);
          }
          if(customStyle.textLineHeight){
            styles_.lineHeight = customStyle.textLineHeight
          }
        }
      }
      return styles_;
    },
    itemConStyle(){
      let unit = getPxOrVw()
      let commonAttr = this.item.commonAttr
      let styles_ = {}

      if(commonAttr.itemBgUrl && commonAttr.itemBgUrl.length > 0){
        styles_.backgroundImage = 'url(' +  commonAttr.itemBgUrl + ')';
        styles_.backgroundRepeat = "no-repeat";
        styles_.backgroundSize = "contain";

        if(commonAttr.itemWidth && commonAttr.itemWidth > 0){
          styles_.width = commonAttr.itemWidth + unit
          styles_.height = commonAttr.itemHeight + unit
        }
      }
      bgPositionStyle(styles_, this.item.commonAttr.bgPositionRepeatType);

      if(commonAttr.itemBGColor && commonAttr.itemBGColor.length > 0){
        styles_.backgroundColor = commonAttr.itemBGColor
      }
      if(commonAttr.itemMarginTB)
        styles_.margin = commonAttr.itemMarginTB + unit + ' ' + commonAttr.itemMarginLR + unit

      if(commonAttr.itemBorderStyle && commonAttr.itemBorderSize > 0){
        styles_.border = commonAttr.itemBorderSize + unit + ' ' +  commonAttr.itemBorderStyle + ' ' + commonAttr.itemBorderColor
      }else{
        styles_.border = "none";
      }
      styles_.padding = commonAttr.itemPaddingTB + unit + ' ' + commonAttr.itemPaddingLR + unit
      return styles_
    },
    itemStyle() {
      let styles_ = {}
      let styles = this.styles
      styles_.color = styles.color
      styles_.lineHeight = styles.lineHeight
      styles_.fontSize = styles.fontSize;
      if(styles.fontWeight){
        styles_.fontWeight = styles.fontWeight;
      }
      if(styles.letterSpacing){
        styles_.letterSpacing = styles.letterSpacing;
      }
      return styles_
    },
    imgStyle(){
      let unit = getPxOrVw()
      let styles_ = {}
      let commonAttr = this.item.commonAttr
      if(commonAttr.itemPicWidth > 0){
        styles_.width = commonAttr.itemPicWidth + unit
        styles_.height = commonAttr.itemPicHeight + unit
      }else{
        styles_.width = "100%"
        styles_.objectFit = "contain"
      }
      if(commonAttr.borderSize){
        let borderStyle = commonAttr.borderStyle ? commonAttr.borderStyle : 'none'
        if(commonAttr.borderStyle && commonAttr.borderStyle != "none"){
          styles_.border = commonAttr.borderSize + unit + ' ' +  borderStyle + ' ' + commonAttr.borderColor
        }else{
          styles_.border = "none"
        }
      }
      if(commonAttr.borderRadius){
        styles_.borderRadius = commonAttr.borderRadius + unit
      }
      return styles_
    },
  },
  methods: {
    getStyles() {
      let styles_ = _.cloneDeep(this.item.styles)
      if(_.isArray(styles_)){
        styles_ = {}
      }
      let commonAttr = this.item.commonAttr
      this.computeStyles(styles_, commonAttr)
      return styles_
    },
    getAttrByCustom(compData, txt) {
      let compId = compData.id;
      const commonAttr = this.commonAttr;
      let controlData, itemData, dataKey, txtOrUrl = '';
      const statisticEvent = compData.events.statisticData || this.item.events.statisticData;
      const interactionEvent = compData.events.interactionData;
      let itemIndex = commonAttr.itemIndex;

      let displayNumMark = 0;
      if(this.item.cid == COMPONENT_TYPES.wb_img){
        dataKey = getCompIdByParam(commonAttr.name);
        displayNumMark = 2;
      }else if(this.item.cid == COMPONENT_TYPES.wb_text){
        // 这里的attr有问题
        displayNumMark = -1;
        dataKey = getCompIdByParam(commonAttr.text);
      }

      if(statisticEvent) {
        let statisticDataType = statisticEvent.comps[0].attrs.value;
        if(statisticDataType == STATISTIC_DATA_TYPES_MAP.weibo2020 || statisticDataType == STATISTIC_DATA_TYPES_MAP.weiboTime){
          controlData = this.controls[compId][statisticEvent.controlId];
          itemData = controlData && controlData.data.elements && controlData.data.elements[itemIndex];
        }
      }else if(interactionEvent) {
        let customStr = interactionEvent.comps[0].attrs.value;
        let lists = compData.interactionData.lists;
        let dimensionLists;
        if(compData.interactionData.isDimension){
          dimensionLists = compData.interactionData.dimensionLists[itemIndex]
        }
        //1971 奶爸段位测验
        //从自定义 数据 的一维或者是二维数组中获取数据
        let relateCompId = getCompIdByParam(interactionEvent.comps[1].attrs.value);
        let relateCompData = this.componentMap[relateCompId];
        if(relateCompId && relateCompData && relateCompData.commonAttr.itemIndex >= 0){
          itemIndex = relateCompData.commonAttr.itemIndex;
        }
        let customObj;
        if(customStr && isJSON(customStr)){
          customObj = JSON.parse(customStr);
          if(isHasOwnProperty(customObj, 'levels')){
            //等级数据，levels:[10,20,30,40],bindData:"num"
            let levels = customObj.levels;
            let bindDataKey = customObj.bindData;
            let num = this.bindData[bindDataKey];

            let index_ = 0;
            let len = levels.length-1;
            for(let i = 0; i < len; i++){
              if(num > levels[i] && num < levels[i+1]){
                index_ = i + 1;
                break
              }
            }
            txtOrUrl = lists[index_].url;
          }else if(isHasOwnProperty(customObj, 'bindData') && customObj.bindData == 'isRights') {
            //isRights 答题是否正确 数组 正确选择 数组下标为 1 的图片url
            let bindDataKey = customObj.bindData;
            let isRights = this.bindData[bindDataKey];
            let index_ = isRights[itemIndex] ? 1 : 0
            txtOrUrl = lists[index_].url;
          } else {
            itemData = this.getItemDataByCompData(compId, compData, commonAttr)
          }
        }else if((lists.length > 0 || dimensionLists) && itemIndex >= 0){
          let listItem;
          if(dimensionLists) {
            lists = dimensionLists[itemIndex];
            if(commonAttr.qIndex >= 0){
              listItem = lists[commonAttr.qIndex];
            } else {
              listItem = lists[_.random(0, lists.length-1)];
            }
          } else {
            listItem = lists[itemIndex];
          }
          if(listItem) {
            if(listItem.text || listItem.url) {
              //1535
              if(this.item.cid == COMPONENT_TYPES.wb_img) {
                txtOrUrl = lists[itemIndex][dataKey] || lists[itemIndex].url;
              } else {
                txtOrUrl = lists[itemIndex][dataKey] || lists[itemIndex].text;
              }
            }
            //1992
            let itemParams = lists[itemIndex].params;
            if(itemParams && isJSON(itemParams)) {
              txtOrUrl = JSON.parse(itemParams).text;
            }
          }
        }
      }else {
        itemData = this.getItemDataByCompData(compId, compData, commonAttr)
      }

      
      if(interactionEvent) {
        const visibleStr = interactionEvent.comps[0].attrs.value;
        this.dealImgVisibility(itemData, displayNumMark, visibleStr);
      }
      

      if(!txtOrUrl && dataKey) {
        if(itemData){
          if(itemData && isHasOwnProperty(itemData, dataKey)) {
            txtOrUrl = itemData[dataKey];
          }
        } else {
          if(_.isArray(this.bindData[dataKey])){
            txtOrUrl = this.bindData[dataKey][itemIndex];
          } else if(this.bindData[dataKey] >= 0 || _.toString(this.bindData[dataKey]).length > 0) {
            txtOrUrl = this.bindData[dataKey];
          }
        }
      }
      if(txt){
        if(txt.indexOf("$") != -1) {
          txtOrUrl = txt.replace("$" + dataKey + "$", txtOrUrl);
        }
      }
      return txtOrUrl
    },
    getDataKeyValue(compData, txt){
      const commonAttr = this.commonAttr;
      const dataKey = commonAttr.dataKey;
      let txtOrUrl = '';
      if(dataKey == "custom"){
        txtOrUrl = this.getAttrByCustom(compData, txt);
      }else{
        const attrs = dataKey.split('-');
        // const eventType = attrs[0];
        let attr = attrs[1];
        let compId = compData.id;
        let itemData = this.getItemDataByCompData(compId, compData, commonAttr)
        if(itemData && isHasOwnProperty(itemData, attr)) {
          txtOrUrl = itemData[attr];
        }
      }
      return txtOrUrl
    },
    dealImgVisibility(itemData_, displayNumMark, visibleStr) {
      let itemData = _.cloneDeep(itemData_);
      const commonAttr = this.commonAttr
      // 1536 图片满足条件才显示 否则就返回 {"isVisible":200,"relateCompId":"87b7187c-5186-4da4-af4d-edb2bca4ffcd"}
      if(isJSON(visibleStr)){
        const visibleObj = JSON.parse(visibleStr);
        //假如有关联组件就取关联组件的数据来对比
        if(isHasOwnProperty(visibleObj, "relateCompId")) {
          let relateCompData = this.componentMap[visibleObj.relateCompId];
          if(relateCompData.events.activityInfo) {
            if(this.activityInfo.isJoin) {
              itemData = { num: 1 };// 伪造的数据用来做对比
            } else {
              itemData = { num: 0 };
            }
          } else if(relateCompData.events.number) {
            itemData = this.controls[relateCompData.id][CONTROL_TYPES.number].data;
          } else {
            let relateCompControlData = this.controls[visibleObj.relateCompId] && this.controls[visibleObj.relateCompId].data;
            if(relateCompControlData) {
              if(isHasOwnProperty(relateCompControlData, "elements")) {
                itemData = relateCompControlData.elements[commonAttr.itemIndex]["num"];
              } else {
                itemData = relateCompControlData;
              }
            }
          }
        }
        if(isHasOwnProperty(visibleObj, "isVisible") && isHasOwnProperty(itemData, "num")){
          let isVisible = false;
          if(itemData.num >= visibleObj.isVisible && commonAttr.itemIndex > displayNumMark){
            isVisible = true;
          }
          if(isVisible != commonAttr.isVisible){
            this.$store.commit("updateComponentAttr", {
              id: this.item.id,
              commonAttr: { isVisible },
            });
          }
          if(isHasOwnProperty(visibleObj, "relateCompId")){
            let isVisibleRelateComp = true;
            if(commonAttr.itemIndex <= 2 || isVisible){
              isVisibleRelateComp = false
            }
            let relateCompData = this.componentMap[visibleObj.relateCompId];
            if(isVisibleRelateComp != relateCompData.commonAttr.isVisible){
              this.$store.commit("updateComponentAttr", {
                id: this.item.id + "-" + commonAttr.itemIndex + "-" + commonAttr.qIndex,
                commonAttr: { isVisible: isVisibleRelateComp },
              });
            }
          }
          return ""
        }
      }
    },
    getItemDataByCompData(compId, compData, commonAttr){
      let itemData = null;
      let mainListEvent = getMainListEventByEvents(compData.events);
      let mainEvent = getMainWbDataEventByEvents(compData.events);
      let controlId = "";
      let controlData = null;
      if(mainListEvent){
        controlId = mainListEvent.controlId;
      }else if(mainEvent){
        if(compData.events.general && compData.events.vote) {
          controlId = CONTROL_TYPES.vote;
        } else {
          controlId = mainEvent.controlId;
        }
      }
      if(controlId && controlId != CONTROL_TYPES.vote){
        controlData = this.controls[compId][controlId];
        if(controlData && controlData.data.elements){
          itemData = controlData.data.elements[commonAttr.itemIndex];
        }else{
          itemData = controlData.data;
        }
      }else{
        itemData = compData.lists && compData.lists[commonAttr.itemIndex];
      }
      return itemData;
    },
    getVariableItemByRelateCompId(relateCompId, eventType){
      let itemControls = this.controls[relateCompId];
      let relateCompData = this.componentMap[relateCompId];
      if(itemControls[eventType].data.elements){
        return itemControls[eventType].data.elements[relateCompData.itemIndex]
      }else{
        return itemControls[eventType].data;
      }
    },
    onJump(e){
      if(this.isH5Edit || (!this.getVariableItem && !this.repostItemData)){
        return
      }

      let shareData = { link: '', midJump: '' };
      let jumpUrl = "";
      if(this.getVariableItem.link){
        jumpUrl = shareData.link = this.getVariableItem.link;
      }else{
        if(isWeibo()){
          //sinaweibo://userinfo?uid=3142205857
          jumpUrl = "sinaweibo://detail/?mblogid=" + this.repostItemData.mid;
          shareData.midJump = jumpUrl;
        }
      }
      this.$store.commit('updateShareData', shareData);

      let comType = "click";
      let wModule = "jump";
      this.onPostStatics(e, { comType, wModule, jumpUrl });


      if(this.item.events.layer){
        //打开弹层 在弹层中打开链接
        return
      }else{
        e.stopPropagation();
      }

      if(jumpUrl){
        this.openLinkBySelfUrl(jumpUrl);
      }
    },
    onMidJump(e, mid){
      e.stopPropagation();

      if(mid){
        let jumpUrl = "sinaweibo://detail/?mblogid=" + mid;

        let comType = "click";
        let wModule = "jump";
        this.onPostStatics(e, { comType, wModule, jumpUrl });

        if(jumpUrl){
          this.openLinkBySelfUrl(jumpUrl);
        }
      }
    },
    openLinkBySelfUrl(jumpUrl) {
      let blocked = false;
      try {
        let worxWin = window.open(jumpUrl);
        if (worxWin == null) {
          blocked = true;
        }
      } catch (ex) {
        blocked = true;
      }
      if (blocked) {
        window.location.href = jumpUrl
      }
    },
    computeStyles(styles_, commonAttr){
      let unit = getPxOrVw()

      if(commonAttr.paddingTB >= 0 || commonAttr.paddingLR >= 0){
        styles_.padding = commonAttr.paddingTB + unit + ' ' + commonAttr.paddingLR + unit
      }
      if(commonAttr.marginTB >= 0 || commonAttr.marginLR >= 0){
        styles_.margin = commonAttr.marginTB + unit + ' ' + commonAttr.marginLR + unit
      }
      if(commonAttr.fontSize > 0){
        styles_.fontSize = commonAttr.fontSize + unit
      }
      if(commonAttr.width && commonAttr.width > 0){
        styles_.width = commonAttr.width + unit
      }
      if(commonAttr.height && commonAttr.height > 0){
        styles_.height = commonAttr.height + unit
      }
      if(commonAttr.lineHeight > 0){
        styles_.lineHeight = commonAttr.lineHeight
      }
      if(commonAttr.letterSpacing > 0){
        styles_.letterSpacing = commonAttr.letterSpacing + unit
      }
      if(commonAttr.fontWeight && commonAttr.fontWeight > 0){
        styles_.fontWeight = commonAttr.fontWeight
      }

      if(isHasOwnProperty(commonAttr, "borderRadius") && commonAttr.borderRadius) {
        styles_.borderRadius = commonAttr.borderRadius + unit
      }
      if(isHasOwnProperty(this, 'isSelected') && this.isSelected){
        styles_.border = 'none'
      }else{
        if(commonAttr.borderStyle && commonAttr.borderSize){
          styles_.border = commonAttr.borderSize + unit + ' ' +  commonAttr.borderStyle + ' ' + commonAttr.borderColor
        }else{
          styles_.border = "none"
        }
      }

      // if(this.item.cid != COMPONENT_TYPES.wb_btn){
        let bgUrl = this.item.commonAttr.bgUrl;
        if(bgUrl && bgUrl.length > 0){
          styles_.backgroundImage = 'url(' + bgUrl + ')';
          styles_.backgroundRepeat = "no-repeat";
          styles_.backgroundSize = "contain";
        }
      // }

      if(commonAttr.customFontStyle){
        let arr = commonAttr.customFontStyle.split(",");
        let fontFamily = arr[0];
        let fontUrl = arr[1];
        styles_.fontFamily = fontFamily;
        let fontStyle = `@font-face {
          font-family: '${fontFamily}';
          src: url('${fontUrl}') format('opentype');
        }`
        this.loadStyleString(fontStyle);
      }
      if(commonAttr.isVertical) {
        styles_.writingMode = "lr-tb";
      }
    },
    // 动态加载css脚本
    loadStyleString(cssText) {
      var style = document.createElement("style");
      style.type = "text/css";
      try{
          // firefox、safari、chrome和Opera
          style.appendChild(document.createTextNode(cssText));
      }catch(ex) {
          // IE早期的浏览器 ,需要使用style元素的stylesheet属性的cssText属性
          style.styleSheet.cssText = cssText;
      }
      document.getElementsByTagName("head")[0].appendChild(style);
    },
    convertCustomCss() {
      let customCssStr = this.commonAttr.customCssStr
      if(customCssStr) {
        let cssArr = convertCss(customCssStr).split(";");
        let keyValueArr;
        let dom = this.getCompDom();
        if(dom) {
          let bgStyle = dom.style;
          let key;
          _.forEach(cssArr, (keyValue) => {
            keyValueArr = keyValue.split(":");
            key = keyValueArr[0].replace(/[\r\n]/g,"");
            bgStyle[key] = keyValueArr[1];
          })
        }
      }
    },
    loadCssOrScript(url, fn) {
      let Head = document.getElementsByTagName('head')[0];
      let linkScript;
      /* eslint-disable */
      if(/\.css[^\.]*$/.test(url)) {
        linkScript = document.createElement("link");
        linkScript.type = "text/" + ("css");
        linkScript.rel = "stylesheet";
        linkScript.href = url;
      } else {
        linkScript = document.createElement("script");
        linkScript.type = "text/" + ("javascript");
        linkScript.src = url;
      }
      Head.insertBefore(linkScript, Head.lastChild)
      linkScript.onload = linkScript.onerror = function() {
        if(fn) fn()
      }
    },
    reload() {
      // this.$forceUpdate()
      // 移除组件
      this.update = false
      // 在组件移除后，重新渲染组件
      // this.$nextTick可实现在DOM 状态更新后，执行传入的方法。
      this.$nextTick(() => {
          this.update = true
      })
    },
    getLink(item){
      let link
      if(item && _.isObject(item)){
        if(isWeibo() && item.schema && item.schema.length > 0){
          link = item.schema
        }else{
          link = item.link
        }
      }else{
        link = item
      }
      return link.length > 0 ? link : 'javascript:;'
    },
    getJumpUrl(jumpUrl){
      return jumpUrl.length > 0 ? jumpUrl : 'javascript:;'
    },
    showAlert(error) {
      this.$dialog.alert({
        title: '',
        message: error,
      })
    },
    getControlDataByEventControlId(controlId) {
      return this.getItemControls[controlId];
    },
    getAttrNameByParam(str){
      let attrName = null;
      if(str && str.length > 0){
        let arr = str.split("$");
        if(arr.length > 1){
          attrName = arr[1];
        }
      }
      return attrName;
    },
    hasClass(elem, cls) {
      cls = cls || '';
      if (cls.replace(/\s/g, '').length == 0) return false; //当cls没有参数时，返回false
      return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' ');
    },
    addClass(elem, cls) {
      //HTML5新增classList
      //如果classList中存在给定的值，删除它，否则，添加它；
      if(elem.classList) elem.classList.add(cls);
    },
    removeClass(elem, cls) {
      if(elem.classList) elem.classList.remove(cls);
    },
    injectDomInCompData(){
      this.item.vueContainer = this;
    },
    showToast(msg) {
      if(msg){
        this.$toast(msg);
      }
    },
    onRefreshCommonAttr() {
      // this.$forceUpdate()
    },
    addCommonAttrListener() {
      EventBus.$on('refreshCommonAttr', this.onRefreshCommonAttr);
    },
    removeCommonAttrListener() {
      EventBus.$off('refreshCommonAttr', this.onRefreshCommonAttr);
    },
  },
  beforeUnmount() {
    EventBus.$off('refreshCommonAttr', this.onRefreshCommonAttr);
  }
}
